<?php
Yii::import('application.components.Reference');
Yii::import('application.components.U');
class SalestransController extends GxController
{
    public function actionCreate()
    {
        if (!Yii::app()->request->isAjaxRequest)
            $this->redirect(url('/'));
        if (isset($_POST) && !empty($_POST)) {
            $msg = "Stored data failed.";
            $detils = CJSON::decode($_POST['detil']);
            app()->db->autoCommit = false;
            $transaction = Yii::app()->db->beginTransaction();
            try {
                $model = new Salestrans;
                $ref = new Reference();
                $docref = $ref->get_next_reference(PENJUALAN);
                foreach ($_POST as $k => $v) {
                    if ($k == 'detil') continue;
                    if (is_angka($v)) $v = get_number($v);
                    $_POST['Salestrans'][$k] = $v;
                }
                $_POST['Salestrans']['doc_ref'] = $docref;
                $_POST['Salestrans']['tgl'] = new CDbExpression('NOW()');
                $model->attributes = $_POST['Salestrans'];
                if (!$model->save())
                    throw new Exception(t('save.model.fail', 'app', array('{model}' => 'Sales')) . CHtml::errorSummary($model));
                foreach ($detils as $detil) {
                    $salestrans_detail = new SalestransDetails;
                    $_POST['SalestransDetails']['barang_id'] = $detil['barang_id'];
                    $_POST['SalestransDetails']['qty'] = get_number($detil['qty']);
                    $_POST['SalestransDetails']['price'] = get_number($detil['price']);
                    $_POST['SalestransDetails']['disc'] = get_number($detil['disc']);
                    $_POST['SalestransDetails']['discrp'] = get_number($detil['discrp']);
                    $_POST['SalestransDetails']['ketpot'] = $detil['ketpot'];
                    $_POST['SalestransDetails']['beauty_id'] = $detil['beauty_id'];
                    $_POST['SalestransDetails']['vat'] = get_number($detil['vat']);
                    $_POST['SalestransDetails']['vatrp'] = get_number($detil['vatrp']);
                    $_POST['SalestransDetails']['total_pot'] = get_number($detil['totalpot']);
                    $_POST['SalestransDetails']['total'] = get_number($detil['total']);
                    $_POST['SalestransDetails']['bruto'] = get_number($detil['bruto']);
                    $_POST['SalestransDetails']['salestrans_id'] = $model->salestrans_id;
                    $salestrans_detail->attributes = $_POST['SalestransDetails'];
                    if (!$salestrans_detail->save())
                        throw new Exception(t('save.model.fail', 'app', array('{model}' => 'Sales Detail')) . CHtml::errorSummary($salestrans_detail));
                    $salestrans_detail->save_beauty_tips();
                    if ($salestrans_detail->barang->grup->kategori_id > 1) {
                        U::add_stock_moves(PENJUALAN, $model->salestrans_id, $model->tgl,
                            $salestrans_detail->barang_id, -$salestrans_detail->qty, $model->doc_ref,
                            $salestrans_detail->price);
                    }
                }
                U::add_bank_trans(PENJUALAN, $model->salestrans_id, $model->bank_id, $model->doc_ref, $model->tgl,
                    $model->total, $model->user_id);
                $ref->save(PENJUALAN, $model->salestrans_id, $docref);
                $transaction->commit();
                $msg = t('save.success', 'app');
                $status = true;
            } catch (Exception $ex) {
                $transaction->rollback();
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'id' => $docref,
                'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id, 'Salestrans');
        if (isset($_POST) && !empty($_POST)) {
            foreach ($_POST as $k => $v) {
                if (is_angka($v)) $v = get_number($v);
                $_POST['Salestrans'][$k] = $v;
            }
            $msg = "Data gagal disimpan";
            $model->attributes = $_POST['Salestrans'];
            if ($model->save()) {
                $status = true;
                $msg = "Data berhasil di simpan dengan id " . $model->salestrans_id;
            } else {
                $msg .= " " . implode(", ", $model->getErrors());
                $status = false;
            }
            if (Yii::app()->request->isAjaxRequest) {
                echo CJSON::encode(array(
                    'success' => $status,
                    'msg' => $msg
                ));
                Yii::app()->end();
            } else {
                $this->redirect(array('view', 'id' => $model->salestrans_id));
            }
        }
    }
    public function actionDelete($id)
    {
        if (Yii::app()->request->isPostRequest) {
            $msg = 'Data berhasil dihapus.';
            $status = true;
            try {
                $this->loadModel($id, 'Salestrans')->delete();
            } catch (Exception $ex) {
                $status = false;
                $msg = $ex;
            }
            echo CJSON::encode(array(
                'success' => $status,
                'msg' => $msg));
            Yii::app()->end();
        } else
            throw new CHttpException(400,
                Yii::t('app', 'Invalid request. Please do not repeat this request again.'));
    }
    public function actionIndex()
    {
        $criteria = new CDbCriteria();
        $criteria->addCondition('total >= 0 AND DATE(tgl) = :tgl');
        $criteria->params = array(':tgl' => $_POST['tgl']);
        $model = Salestrans::model()->findAll($criteria);
        $total = Salestrans::model()->count($criteria);
        $this->renderJson($model, $total);
    }
    public function actionBeautyService()
    {
        if (Yii::app()->request->isPostRequest) {
            $tgl = $_POST['tgl'];
            $arr = Salestrans::list_beuty_service($tgl);
            $this->renderJsonArr($arr);
            Yii::app()->end();
        } else
            throw new CHttpException(403,
                Yii::t('app', 'Invalid request. Please do not repeat this request again.'));
    }
}